//
//  HomeViewController.m
//  09_Work 通讯录
//
//  Created by dllo on 16/7/25.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "HomeViewController.h"
#define kTableViewReuse @"reuse"
#import "DetailViewController.h"
#import "AddViewController.h"

@interface HomeViewController () <UITableViewDataSource, UITableViewDelegate, passDic>
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation HomeViewController

//在昨天作业基础上, 添加联系人删除功能
//编写省 推出-> 市 推出-> 区
//编写分区是省, cell是市, 点击市推出区的页面

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createTableView];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addClick:)];
}
// 通讯录添加删除功能
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.array removeObjectAtIndex:indexPath.row];
    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

- (void)addClick:(UIButton *)button {
    AddViewController *add = [[AddViewController alloc] init];
    add.delegate = self;
    [self.navigationController pushViewController:add animated:YES];
}

- (void)passDic:(NSMutableDictionary *)dic {
    [self.array addObject:dic];
    [self.tableView reloadData];
}

- (void)createTableView {
    self.array = [NSMutableArray array];
    self.navigationController.navigationBar.barTintColor = [UIColor grayColor];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"DLS160710(1)" ofType:@"plist"];
    self.array = [NSMutableArray arrayWithContentsOfFile:path];

    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kTableViewReuse];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewReuse];
    cell.textLabel.text = [self.array[indexPath.row] objectForKey:@"name"];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DetailViewController *detail = [[DetailViewController alloc] init];
    detail.nameStr = [self.array[indexPath.row] objectForKey:@"name"];
    detail.age = [self.array[indexPath.row] objectForKey:@"age"];
    detail.sexStr = [self.array[indexPath.row] objectForKey:@"gender"];
    detail.phoneStr = [self.array[indexPath.row] objectForKey:@"phoneNumber"];
    
    [self.navigationController pushViewController:detail animated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
